package Negocios;

import CDB.PersonFactory;
import CDB.Proveedores;
import CDB.Stock;
import java.awt.HeadlessException;
import java.util.List;
import javax.swing.JOptionPane;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

/**
 *
 * @author lightemp
 */
public class ControlStock {

    private Session session;
    private Transaction transaction;
    private SessionFactory sessionFactory;

    public ControlStock() {
    }

    public List selectAll() {
        List list = null;
        try {
            Session session = createSession();
            //Creacion de query para obtener todos los empleados
            list = session.createQuery("from Stock").list();
            session.close();
            //Retorno la lista con todos los empleados
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "error: " + e, "Ha ocurrido un error.",
                    0);
        }

        return list;
    }

    public Stock selectOne(int id) {
        Stock stock;
        try {
            iniciaSF();
            stock = (Stock) session.createQuery("from Stock s where s.id='" + id + "'").list().get(0);
            cierraSF();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "error: " + e, "Ha ocurrido un error", 0);
            return null;
        }
        return stock;
    }

    public Stock selectOneByName(String nombre) {
        Stock stock = null;
        try {
            Session session = createSession();
            stock = (Stock) session.createQuery("from Stock s where s.nombre='" + nombre + "'").list().get(0);
            session.close();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "error: " + e, "Ha ocurrido un error", 0);
            throw e;
        }
        return stock;
    }

    public Stock selectOneByNameToStock(String nombre) {
        Stock stock = null;
        try {
            Session session = createSession();
            stock = (Stock) session.createQuery("from Stock s where s.nombre='" + nombre + "'").list().get(0);
            session.close();
        } catch (Exception e) {
            throw e;
        }
        return stock;
    }

    public void add(String nombre, int cantidad, short minimo, float precio, Proveedores proveedor) {
        try {
            iniciaSF();

            //Creando un objeto
            Stock stock = new Stock();
            stock.setNombre(nombre);
            stock.setCantidad(cantidad);
            stock.setMinimo(minimo);
            stock.setPreciogranel(precio);
            stock.setProveedores(proveedor);
            session.save(stock);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "error: " + e, "Ha ocurrido un error", 0);
        } finally {
            cierraSF();
        }

    }

    public void Update(int id, String nombre, int cantidad, short minimo, float precio, Proveedores proveedor) {
        try {
            iniciaSF();

            Stock stock = (Stock) session.load(Stock.class,
                    new Integer(id));

            //Creando un objeto
            stock.setNombre(nombre);
            stock.setCantidad(cantidad);
            stock.setMinimo(minimo);
            stock.setPreciogranel(precio);
            stock.setProveedores(proveedor);
            session.update(stock);

        } catch (HibernateException e) {
            JOptionPane.showMessageDialog(null, "error: " + e, "Ha ocurrido un error", 0);
        } finally {
            cierraSF();
        }
    }

    public void delete(int id) {
        try {
            iniciaSF();

            Stock stock = (Stock) session.load(Stock.class,
                    new Integer(id));
//            session.createQuery("delete from Stock s where s.idstock="+id);
            session.delete(stock);
        } catch (HibernateException e) {
            JOptionPane.showMessageDialog(null, "error: " + e, "Ha ocurrido un error", 0);
        } finally {
            cierraSF();
        }

    }

    public void deleteByObject(Stock stock) {
        try {
            iniciaSF();
            session.delete(stock);
        } catch (HibernateException e) {
            JOptionPane.showMessageDialog(null, "error: " + e, "Ha ocurrido un error", 0);
        } finally {
            cierraSF();
        }

    }

    public void iniciaSF() {
        try {
            session = null;
            sessionFactory = PersonFactory.getSessionFactory();
            session = sessionFactory.openSession();
            transaction = session.beginTransaction();
        } catch (HibernateException e) {
            JOptionPane.showMessageDialog(null, "error: " + e + "\n"
                    + "En el controlador de stock.", "Error al iniciar Session",
                    0);
        }

    }

    public void cierraSF() {
        try {
            transaction.commit();
            session.flush();
            session.close();
        } catch (HibernateException e) {
            JOptionPane.showMessageDialog(null, "error: " + e + "\n"
                    + "En el controlador de stock.", "Error al cerrar Session",
                    0);
            System.out.println(e);
        }
    }

    public Session createSession() {

        Session session = null;
        try {
            //creando la nueva session para usar hibernate
            sessionFactory = PersonFactory.getSessionFactory();
            session = sessionFactory.openSession();

        } catch (HibernateException e) {
            JOptionPane.showMessageDialog(null, "error: " + e + "\n"
                    + "En el controlador de stock.", "Error al crear Session",
                    0);
            session.close();
        }
        return session;
    }
}
